package com.example.dllo.my36kr.ui.activity;

import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.ui.adapter.MainTabAdapter;
import com.example.dllo.my36kr.ui.fragment.FindFragment;
import com.example.dllo.my36kr.ui.fragment.InvestFragment;
import com.example.dllo.my36kr.ui.fragment.MessageFragment;
import com.example.dllo.my36kr.ui.fragment.MineFragment;
import com.example.dllo.my36kr.ui.fragment.NewsFragment;

import java.util.LinkedList;
import java.util.List;

public class MainActivity extends AbsBaseActivity {
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private List<Fragment>datas;

    @Override
    protected int setLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
      viewPager=byView(R.id.main_view_pager);
        tabLayout=byView(R.id.tab_main);
    }
      //onRestart（）是activity的一个生命周期，基类中就只做了一件事情就是设置了一下mCalled设置为true代表界面当前使用状态
    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void initData() {
      addFragment();
    }
    private  void addFragment(){
        //LinkedList类是双向列表,列表中的每个节点都包含了对前一个和后一个元素的引用.
        datas=new LinkedList<>();
        datas.add(new NewsFragment());//新闻
        datas.add(new InvestFragment());//股权投资
        datas.add(new FindFragment());//发现
        datas.add(new MessageFragment());//消息
        datas.add(new MineFragment());//我的
        MainTabAdapter adapter =new MainTabAdapter(getSupportFragmentManager(),datas);
        viewPager.setAdapter(adapter);
        //tabLayout和viewpager关联
        tabLayout.setupWithViewPager(viewPager);
        //设置图片
        setTabIcon();
        //设置选定的标签显示颜色
        tabLayout.setSelectedTabIndicatorColor(Color.WHITE);
        // 内容的显示模式 fill，则是充满
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        //设置选中指示器下标的高度：
        tabLayout.setSelectedTabIndicatorHeight(0);
        //改变选中字体的颜色
        tabLayout.setTabTextColors(Color.rgb(0x98, 0x9d, 0xa8), Color.rgb(0x6c, 0x8f, 0xff));
    }
    //给tablayout设置图片
    private void setTabIcon() {
        int[] ids = {R.drawable.selector_news, R.drawable.selector_invest, R.drawable.selector_find, R.drawable.selector_message, R.drawable.selector_mine};
        for (int i = 0; i < ids.length; i++) {
            tabLayout.getTabAt(i).setIcon(ids[i]);
        }
    }
}
